<template>
  <el-container class="panel-block">
    <el-header class="sa-header">
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">
          <span class="left">商品库管理</span>
          <search-condition
            :conditionLabel="filterParams.conditionLabel"
            @deleteFilter="deleteFilter"
          ></search-condition>
        </div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="getData()"></el-button>
          <el-button class="sa-button-refresh" icon="Search" @click="openFilter"></el-button>
          <el-button
            v-auth="'shop.admin.app.mplive.goods.add'"
            class="sa-button-refresh"
            type="primary"
            icon="Plus"
            @click="addGoods"
            >添加商品</el-button
          >
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0" v-loading="loading">
      <el-table height="100%" class="sa-table" :data="goods.data" stripe>
        <template #empty>
          <sa-empty />
        </template>
        <el-table-column prop="id" label="ID" min-width="80"></el-table-column>
        <el-table-column label="商品来源" min-width="120" align="center">
          <template #default="scope">
            <div class="sa-table-line-1">{{ scope.row.type_text }}</div>
          </template>
        </el-table-column>
        <el-table-column label="商品封面" min-width="80" align="center">
          <template #default="scope">
            <div class="sa-flex sa-row-center">
              <sa-preview :url="scope.row.cover_img_url" size="30"></sa-preview>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" min-width="220" align="center">
          <template #default="scope">
            <div class="sa-table-line-1">{{ scope.row.name }}</div>
          </template>
        </el-table-column>
        <el-table-column label="价格形式" min-width="120" align="center">
          <template #default="scope">
            <div class="sa-table-line-1">{{ scope.row.price_type_text }}</div>
          </template>
        </el-table-column>

        <el-table-column label="价格" min-width="200" align="center">
          <template #default="scope">
            <div class="sa-table-line-1">
              <div class="sa-flex sa-row-center">
                <div :class="{ 'price-title': scope.row.price_type === 3 }"
                  >{{ scope.row.price }}元</div
                >
                <div>
                  {{ formatPrice(scope.row.price2, scope.row.price_type) }}
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="商品路径" min-width="300" align="center">
          <template #default="scope">
            <div class="sa-table-line-1">{{ scope.row.url }}</div>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" min-width="120" align="center" fixed="right">
          <template #default="scope">
            <div class="sa-flex sa-row-center">
              <div
                class="sa-table-line-1"
                :class="
                  scope.row.audit_status === 2
                    ? 'sa-color--success'
                    : scope.row.audit_status === 3
                    ? 'sa-color--danger'
                    : scope.row.audit_status === 1
                    ? 'sa-color--warning'
                    : 'sa-color--info'
                "
                >{{ scope.row.audit_status_text }}</div
              >
              <el-button
                class="is-link"
                icon="RefreshRight"
                @click="getStatus(scope.row.id)"
              ></el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="200" fixed="right">
          <template #default="scope">
            <el-button
              v-auth="'shop.admin.app.mplive.goods.detail'"
              class="is-link"
              type="primary"
              @click="editGoods(scope.row.id)"
              >编辑</el-button
            >
            <el-button
              v-auth="'shop.admin.app.mplive.goods.audit'"
              class="is-link"
              type="primary"
              @click="check(scope.row.id, 'resubmit')"
              v-if="scope.row.audit_status === 0 || scope.row.audit_status === 3"
              >提交审核</el-button
            >
            <el-button
              v-auth="'shop.admin.app.mplive.goods.audit'"
              class="is-link"
              type="primary"
              @click="check(scope.row.id, 'reset')"
              v-if="scope.row.audit_status === 1"
              >撤回审核</el-button
            >
            <el-popconfirm
              width="fit-content"
              confirm-button-text="确认"
              cancel-button-text="取消"
              title="确认删除这条记录?"
              @confirm="deleteGoods(scope.row.id)"
            >
              <template #reference>
                <el-button
                  v-auth="'shop.admin.app.mplive.goods.delete'"
                  class="is-link"
                  type="danger"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <sa-view-bar>
      <template #right>
        <sa-pagination :pageData="pageData" @updateFn="getData" />
      </template>
    </sa-view-bar>
  </el-container>
</template>
<script>
  export default {
    name: 'shop.admin.mplive.goods',
  };
</script>
<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { api } from '../../app.service';
  import GoodsEdit from './edit.vue';
  import { useModal, usePagination } from '@/sheep/hooks';
  import { useSearch } from '@/sheep/components/sa-table/sa-search/useSearch';
  import { composeFilter } from '@/sheep/utils';
  import { cloneDeep } from 'lodash';

  const loading = ref(true);
  function formatPrice(price, type) {
    if (type === 1) {
      return '';
    } else if (type === 2) {
      return '~' + price + '元';
    } else {
      return price + '元';
    }
  }
  const filterParams = reactive({
    tools: {
      name: {
        type: 'tinput',
        field: 'name',
        value: '',
        label: '商品名称',
        placeholder: '请输入商品名称',
      },
    },
    data: {
      name: '',
    },
    conditionLabel: {},
  });
  const { openFilter, deleteFilter } = useSearch({ filterParams, getData });
  const { pageData } = usePagination();

  //商品库表格
  const goods = reactive({
    data: [],
    order: '',
    sort: '',
  });
  // 获取商品库数据
  async function getData(page) {
    loading.value = true;
    if (page) pageData.page = page;
    let tempSearch = cloneDeep(filterParams.data);
    let search = composeFilter(tempSearch, {
      name: 'like',
    });
    const { error, data } = await api.mplive.goods.list({
      page: pageData.page,
      list_rows: pageData.list_rows,
      order: goods.order,
      sort: goods.sort,
      ...search,
    });
    if (error === 0) {
      pageData.page = data.current_page;
      pageData.list_rows = data.per_page;
      pageData.total = data.total;
      goods.data = data.data;
    }
    loading.value = false;
  }

  //添加商品
  function addGoods() {
    useModal(
      GoodsEdit,
      {
        title: '添加商品',
        type: 'add',
      },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }
  //编辑商品
  function editGoods(id) {
    useModal(
      GoodsEdit,
      {
        title: '编辑商品库',
        type: 'edit',
        id: id,
      },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }
  // 删除商品
  async function deleteGoods(id) {
    loading.value = true;
    await api.mplive.goods.delete(id);
    loading.value = false;
    getData();
  }
  //刷新状态
  async function getStatus(id) {
    const { error } = await api.mplive.goods.status(id);
    if (error === 0) {
      getData();
    }
  }

  //审核
  async function check(id, act) {
    const { error } = await api.mplive.goods.audit(id, {
      act,
    });
    if (error === 0) {
      getData();
    }
  }

  onMounted(() => {
    getData();
  });
</script>
<style lang="scss" scoped>
  .price-title {
    text-decoration: line-through;
    margin-right: 8px;
    color: var(--sa-subfont);
  }
  .live-qrcode {
    width: 150px;
    height: 150px;
  }
</style>
